package com.gyms;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.view.View;

import com.classic.okhttp.Cons;
import com.classic.okhttp.beans.HVCitiesBean;
import com.classic.okhttp.beans.HVProjectBean;
import com.gyms.manager.DataCenter;
import com.gyms.manager.GlideImageLoader;
import com.gyms.manager.StatisticsManager;
import com.gyms.view.swipeback.ActivityLifecycleHelper;
import com.igexin.sdk.PushManager;
import com.orhanobut.logger.Logger;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.util.List;

import cn.finalteam.galleryfinal.CoreConfig;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.ThemeConfig;
import utils.AppUtils;
import utils.DeviceUtils;

import static com.classic.okhttp.Cons.mDeviceId;

/**
 * 自定义Application
 * 用于初始化各种数据以及服务
 */
public class MyApplication extends MultiDexApplication {
    /**
     * 城市的bean
     */
    public HVCitiesBean mHVCitiesBean;
    /**
     * 全部运动的bean
     */
    public List<HVProjectBean> mHVProjectBean;

    /**
     * 应用实例
     **/
    private static MyApplication instance;

    private final static String mWxAppId="wx7d8e0d35db4505dd";
    private final static String mWxSecret="a8f17e93fb546f605921932b961d47cb";

    private final static String mQQAppId="1105942436";
    private final static String mQQKey="gE7SoReGNYK7iXwm";

    public UMShareAPI mUMShareAPI;



    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        if (null == DataCenter.getInstance())
            DataCenter.initDataCenter();
        registerActivityLifecycleCallbacks(mActivityLifecycleHelper = new ActivityLifecycleHelper());

        //设置主题
        ThemeConfig theme = new ThemeConfig.Builder()
                .setTitleBarBgColor(Color.rgb(0x03, 0x96, 0xeb))
                .setFabNornalColor(Color.rgb(0x00, 0xac, 0xc1))
                .setFabPressedColor(Color.rgb(0x01, 0x83, 0x93))
                .setCheckSelectedColor(Color.rgb(0x03, 0x96, 0xeb))
                .setCropControlColor(Color.rgb(0x03, 0x96, 0xeb))
                .build();
        //配置功能
        FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setEnableEdit(true)
                .setEnableCrop(true)
                .setEnableRotate(true)
                .setCropSquare(false)
                .setEnablePreview(true)
                .build();
        CoreConfig coreConfig = new CoreConfig.Builder(this, new GlideImageLoader(), theme)
                .setNoAnimcation(true)
                .setFunctionConfig(functionConfig)
                .build();
        GalleryFinal.init(coreConfig);
        initApp();
        PushManager.getInstance().initialize(this.getApplicationContext(),com.gyms.service.MyPushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), com.gyms.service.IntentService.class);

        StatisticsManager.getInstance().initUmeng(getApplicationContext());
        //开启debug模式，方便定位错误，具体错误检查方式可以查看http://dev.umeng.com/social/android/quick-integratio的报错必看，正式发布，请关闭该模式
        Config.DEBUG = false;
        mUMShareAPI=UMShareAPI.get(this);
        //初始化扫码
        ZXingLibrary.initDisplayOpinion(this);
    }

    //友盟分享
    {
        PlatformConfig.setWeixin(mWxAppId, mWxSecret);
        PlatformConfig.setQQZone(mQQAppId,mQQKey);
    }

    /**
     * 初始化网络相关
     */

    private void initApp() {
        //deviceId
        mDeviceId= DeviceUtils.getInstance(getApplicationContext()).getID();
        Logger.e("mDeviceId="+ mDeviceId);
        Cons.mVersion= AppUtils.getAppVersionName(getApplicationContext());
        Logger.e("Version="+Cons.mVersion);
        Cons.mChanel=AppUtils.getAppMetaData(getApplicationContext(),"TD_CHANNEL_ID");
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 获得实例
     *
     * @return
     */
    public static MyApplication getInstance() {
        return instance;
    }
    //-------------------------滑动返回Star--------------------------
    private ActivityLifecycleHelper mActivityLifecycleHelper;
    public ActivityLifecycleHelper getActivityLifecycleHelper() {
        return mActivityLifecycleHelper;
    }
    public void onSlideBack(boolean isReset, float distance) {
        if(mActivityLifecycleHelper != null) {
            Activity lastActivity = mActivityLifecycleHelper.getPreActivity();
            if(lastActivity != null) {
                View contentView = lastActivity.findViewById(android.R.id.content);
                if(isReset) {
                    contentView.setX(contentView.getLeft());
                } else {
                    final int width = getResources().getDisplayMetrics().widthPixels;
                    contentView.setX(-width / 3 + distance / 3);
                }
            }
        }
    }
    //-------------------------滑动返回End--------------------------

}
